package com.jie.mc.commands;

import com.jie.mc.Main;
import com.jie.mc.gui.renqiInventory;
import com.jie.mc.util.JsonMessage;
import com.jie.mc.util.TitleApi;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Bat;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Arrays;

public class MyCommand implements CommandExecutor {
/*
    private JavaPlugin plugin;

    public MyCommand(JavaPlugin plugin) {
        this.plugin = plugin;
    }
*/

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (label.equalsIgnoreCase("renqi")) {
                if (args.length == 2) {
                    if (args[0].equalsIgnoreCase("up")) {
                        if (player.hasPermission("renqi.up")) {
                            renqiInventory.PlayerrenqiGui(player);
                            /*Location location = player . getLocation();
                            player . sendMessage(  "X坐标:"+location . getX()+"Y坐标:"+location. getY()+"Z坐标:"+location. getZ()) ;
                            Location world = new Location(Bukkit.getWorld("world"), 10, 100, 10);
                            player.teleport(world);*/
                        } else {
                            sender.sendMessage("§e没有renqi.up权限");
                        }
                    } else if (args[0].equalsIgnoreCase("down")) {
                        if (player.hasPermission("renqi.down")) {
                            if(args[1].equalsIgnoreCase("1")) {
                                System.out.println("=============");
                                TitleApi.sendTitle(player,5,4,5,"大标题","小标题");
                            }else if(args[1].equalsIgnoreCase("2"))
                                TitleApi.sendTitle(player,5,4,5,"大大标题","小小标题");
                            if(args[1].equalsIgnoreCase("3")) {
                                new BukkitRunnable() {
                                    int s = 10;

                                    @Override
                                    public void run() {
                                        s--;
                                        if (s == 0) {
                                            cancel();
                                        } else {
                                            Location location = player.getLocation();
                                            for (int degree = 0; degree < 360; degree++) {
                                                double radians = Math.toRadians(degree);
                                                double x = Math.cos(radians);
                                                double y = Math.sin(radians);
                                                 location.add(x, s, y);
                                                location.getWorld().playEffect(location, Effect.HAPPY_VILLAGER, 1);
                                                location.subtract(x, s, y);
                                            }
                                        }


                                    }

                                    ;
                                }.runTaskTimer(Main.plugin, 0, 20);
                            }
                            /*World world = player.getWorld();
                            Bat bat = (Bat)world.spawnEntity(player.getLocation(), EntityType.BA T);
                            bat.setCustomNameVisible(true);
                            bat.setCustomName("吸血鬼");*/
                            /*if(args[1].equalsIgnoreCase("3")) {
                                Location location=player.getLocation();
                                for (int degree = 0; degree <360 ;degree++) {
                                    double radians = Math.toRadians(degree);
                                    double x= Math.cos(radians);
                                    double y= Math.sin(radians);
                                    location.add(x,0,y);
                                    location.getWorld().playEffect(location, Effect.HAPPY_VILLAGER,1);
                                    location.subtract(x,0,y);
                                }
                            }*/
                        } else {
                            sender.sendMessage("§e没有renqi.down权限");
                        }
                    }
                } else if (args.length == 3) {
                    if (args[0].equalsIgnoreCase("add")) {
                        if (player.hasPermission("renqi.add")) {

                        } else {
                            sender.sendMessage("§e没有renqi.add权限，管理员才有这个权限");
                        }
                    } else if (args[0].equalsIgnoreCase("remove")) {
                        if (player.hasPermission("renqi.remove")) {
                        } else {
                            sender.sendMessage("§e没有renqi.remove权限，管理员才有这个权限");
                        }

                    }
                    else if (args[0].equalsIgnoreCase("set")) {
                        if (player.hasPermission("renqi.set")) {
                        } else {
                            sender.sendMessage("§e没有renqi.set权限，管理员才有这个权限");
                        }
                        sender.sendMessage("§设置§e成功");
                    }
                    /*if (args[0].equalsIgnoreCase("add")) {

                        for (Player aplayer : Bukkit.getServer().getOnlinePlayers())
                            aplayer.getLocation();
                        JsonMessage.sendRawJson("[\"\",{\"text\":\"点我访问百度\",\"bold\":true,\"color\":\"dark_red\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.baidu.com/\"},\"hoverEvent\":{\"action\":\"show_text\",\"contents\":[\"\",{\"text\":\"我是悬浮内容\",\"bold\":true,\"color\":\"aqua\"}]}}]", player);
                        sender.sendMessage("§d增加人气§e成功");
                    } else if (args[0].equalsIgnoreCase("remove")) {
                        sender.sendMessage("§d减少人气§e成功");

                    }*/
                }


            }
        } else {
            sender.sendMessage("服务器命令行不能用这个命令");
        }
        return true;
    }
}
